package com.xunshangwang.pinhaohuo.ui.fragments.address;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xunshangwang.pinhaohuo.adapters.AddressNameAdapter;
import com.xunshangwang.pinhaohuo.ui.activities.address.SelectAddressActivity;

import java.util.List;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 16/2/16.
 */
public class DistrictFragment extends BaseAddressFragment{
    public static final DistrictFragment newInstance(int provincePos,int cityPos){
        DistrictFragment fragment=new DistrictFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("provincePos",provincePos);
        bundle.putInt("cityPos", cityPos);
        fragment.setArguments(bundle);
        return fragment;
    }
    private int mProvincePos,mCityPos;
    private DistrictAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle=getArguments();
        if(bundle!=null){
            mProvincePos=bundle.getInt("provincePos");
            mCityPos=bundle.getInt("cityPos");
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SelectAddressActivity activity= (SelectAddressActivity) getActivity();
        SelectAddressActivity.City city=activity.getProvinceList().get(mProvincePos).getCityList().get(mCityPos);
        mAdapter=new DistrictAdapter(getContext(),city.getDistrictList());
        mAdapter.setOnItemClickListener(this);
        mRecycler.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(BaseRecyclerAdapter adapter, View item, int position) {
        SelectAddressActivity activity= (SelectAddressActivity) getActivity();
        Intent intent=new Intent();
        SelectAddressActivity.Province province=activity.getProvinceList().get(mProvincePos);
        SelectAddressActivity.City city=province.getCityList().get(mCityPos);
        SelectAddressActivity.District district=city.getDistrictList().get(position);
        intent.putExtra("address",province.getName()+"-"+city.getName()+"-"+district.getName());
        intent.putExtra("provinceId",province.getId());
        intent.putExtra("cityId",city.getId());
        intent.putExtra("districtId",district.getId());
        activity.setResult(Activity.RESULT_OK,intent);
        activity.finish();
    }
    private class DistrictAdapter extends AddressNameAdapter<SelectAddressActivity.District>{
        public DistrictAdapter(Context context, List list) {
            super(context, list);
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            NameHolder nameHolder= (NameHolder) holder;
            nameHolder.nameText.setText(getList().get(position).getName());
        }
    }
}
